package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import dao.User_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;


/**
 * The controller keeps all database operations behind the repository, and uses
 * {@link HttpExecutionContext} to provide access to the
 * {@link play.mvc.Http.Context} methods like {@code request()} and {@code flash()}.
 */
public class UserController extends Controller {

    private final FormFactory formFactory;
    private final User_Repository userRepository;
    private final HttpExecutionContext ec;

    @Inject
    public UserController(FormFactory formFactory, User_Repository userRepository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.userRepository = userRepository;
        this.ec = ec;
    }
    
    public CompletionStage<Result> login() {
		JsonNode json = request().body().asJson();
		String mobile = json.findPath("mobile").textValue();
		String password = json.findPath("password").textValue();
		String Imei = json.findPath("IMEI").textValue();
	    return userRepository.login(mobile, password,Imei).thenApplyAsync(_user -> {
	    	ObjectNode result = Json.newObject();
//    		result.put("userid", _user.getFdUserId());
    		return ok(Json.toJson(_user));
	    }, ec.current());
    }
	public CompletionStage<Result> logout() {
		JsonNode json = request().body().asJson();
		String mobile = json.findPath("mobile").textValue();
		return userRepository.logout(mobile).thenApplyAsync(_user -> {
			ObjectNode result = Json.newObject();
//    		result.put("userid", _user.getFdUserId());
			return ok(Json.toJson(_user));
		}, ec.current());
	}


    public CompletionStage<Result> getUserList() {
	    return userRepository.getUserList().thenApplyAsync(_user -> {
	    	return ok(Json.toJson(_user));
	    }, ec.current());
    }

    public CompletionStage<Result> CreateUser(){
		JsonNode json;
		json = request().body().asJson();
		String type=json.findPath("type").textValue();
		String fdUserName = json.findPath("fdUserName").textValue();
//		String fdImei = json.findPath("fdImei").textValue();
//		String fdPasswordHash = json.findPath("fdPasswordHash").textValue();
		String fdDeptId = json.findPath("fdDeptId").textValue();
//		String fdRegisterDate = json.findPath("fdRegisterDate").textValue();
//		String fdLastLoginDate = json.findPath("fdLastLoginDate").textValue();
//		String fdToken = json.findPath("fdToken").textValue();
		String fdMobile = json.findPath("fdMobile").textValue();
//		String deleted = json.findPath("deleted").textValue();
//		String fd_MemberNo = json.findPath("fd_MemberNo").textValue();
//		String fd_SIMNO = json.findPath("fd_SIMNO").textValue();
//		String fd_GPSProvider = json.findPath("fd_GPSProvider").textValue();
		
	    return userRepository.CreateUser(type,fdUserName, fdMobile, fdDeptId ).thenApplyAsync(_GetListByProc -> {
//	    	ObjectNode result = Json.newObject();
    		return ok(Json.toJson(_GetListByProc));
	    }, ec.current());
    }

	public CompletionStage<Result> ModifyPassword() {
		        JsonNode json;
		      json = request().body().asJson();
		     String   phoneNumber= json.findPath("phoneNumber").textValue();
		     String oldPassword= json.findPath("oldPassword").textValue();
		     String newPassword= json.findPath("newPassword").textValue();

		return userRepository.ModifyPassword(phoneNumber,oldPassword,newPassword).thenApplyAsync(_user -> {
			return ok(Json.toJson(_user));
		}, ec.current());
	}

}
